﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PeoplesJukebox.Core.Extensions
{
    /// <summary>
    /// Extension methods for <see cref="Object"/>
    /// </summary>
    public static class ObjectExtensions
    {
        /// <summary>
        /// Lazy-loads a property
        /// </summary>
        /// <typeparam name="T">Type of property</typeparam>
        /// <param name="obj">Extended object</param>
        /// <param name="field">Property backing field</param>
        /// <param name="load">Property loading function</param>
        /// <returns>Lazy-loaded property</returns>
        public static T LazyLoad<T>(this object obj, ref T field, Func<T> load)
        {
            if (field == null || field.Equals(default(T)))
            {
                field = load();
            }
            return field;
        }
    }
}
